package com.zisu.rabbitconsumer.component.equipment;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * @Desc: "CSS设备 接收MQ消息 监听类"
 * @Author: caixiang
 * @DATE: 2021/6/22 15:30
 */
@Component
public class CssReceived {
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="MES_Request_CSS_Queue",durable = "true",
                            arguments = {
                                    //1. 死信队列Demo
                                    @Argument(name = "x-dead-letter-exchange", value = "Dead_Letter_Exchange"),
                                    //可以为死信 指定routingKey，如果不指定routingKey的话 那么 会用原routingKey也就是 eq5_request
                                    @Argument(name = "x-dead-letter-routing-key", value = "Dead_Letter_Queue_CSS_RoutingKey"),
                                    //这里设置的优先级 是这个队列承认的 最高优先级值，也就是说超过10 就 都一样。

                                    //2.优先级队列Demo
                                    @Argument(name = "x-max-priority", value = "10",type = "java.lang.Integer")
                                    //设置队列最大长度
                                    //@Argument(name = "x-max-length", value = "500", type = "java.lang.Integer")
                            }
                    ),
                    exchange = @Exchange(name = "CSS_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "MES_Request_CSS_Queue_RoutingKey"
            )
    )
    @RabbitHandler
    public void mesRequest(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============CSS_Exchange-MES_Request_Queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(100);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        //模拟异常 ，然后 拒签消息 ，然后丢到死信队列
        try {
            System.err.println("处理消息的时候 出现异常，然后 拒签消息 ，然后丢到死信队列");
            throw new Exception("12333");
        }catch (Exception e){
            // 第一个false 是 不批量签收；第二个false 是 不重回队列
            channel.basicNack(deliveryTag,false,false);
        }

        //注意 这里特别注意 已经拒收的消息 再签收是要出错的，这里要特别注意。
        //channel.basicAck(deliveryTag,false);

    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="MES_Reponse_CSS_Queue",durable = "true",
                            arguments = {
                                    @Argument(name = "x-dead-letter-exchange", value = "Dead_Letter_Exchange"),
                                    @Argument(name = "x-dead-letter-routing-key", value = "Dead_Letter_Queue_CSS_RoutingKey"),
                                    @Argument(name = "x-max-priority", value = "10",type = "java.lang.Integer")
                            }
                    ),
                    exchange = @Exchange(name = "CSS_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "MES_Reponse_CSS_RoutingKey"
            )
    )
    @RabbitHandler
    public void mesResponse(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============CSS_Exchange-MES_Reponse_Queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(100);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        //模拟异常 ，然后 拒签消息 ，然后丢到死信队列
        try {
            System.err.println("处理消息的时候 出现异常，然后 拒签消息 ，然后丢到死信队列");
            throw new Exception("12333");
        }catch (Exception e){
            // 第一个false 是 不批量签收；第二个false 是 不重回队列
            channel.basicNack(deliveryTag,false,false);
        }

        //注意 这里特别注意 已经拒收的消息 再签收是要出错的，这里要特别注意。
        //channel.basicAck(deliveryTag,false);

    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="EAP_Request_CSS_Queue",durable = "true",
                            arguments = {
                                    @Argument(name = "x-dead-letter-exchange", value = "Dead_Letter_Exchange"),
                                    @Argument(name = "x-dead-letter-routing-key", value = "Dead_Letter_Queue_CSS_RoutingKey"),
                                    @Argument(name = "x-max-priority", value = "10",type = "java.lang.Integer")
                            }
                    ),
                    exchange = @Exchange(name = "CSS_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "EAP_Request_CSS_RoutingKey"
            )
    )
    @RabbitHandler
    public void eapRequest(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============CSS_Exchange-EAP_Request_Queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(100);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        //模拟异常 ，然后 拒签消息 ，然后丢到死信队列
        try {
            System.err.println("处理消息的时候 出现异常，然后 拒签消息 ，然后丢到死信队列");
            throw new Exception("12333");
        }catch (Exception e){
            // 第一个false 是 不批量签收；第二个false 是 不重回队列
            channel.basicNack(deliveryTag,false,false);
        }

        //注意 这里特别注意 已经拒收的消息 再签收是要出错的，这里要特别注意。
        //channel.basicAck(deliveryTag,false);

    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="EAP_Reponse_CSS_Queue",durable = "true",
                            arguments = {
                                    @Argument(name = "x-dead-letter-exchange", value = "Dead_Letter_Exchange"),
                                    @Argument(name = "x-dead-letter-routing-key", value = "Dead_Letter_Queue_CSS_RoutingKey"),
                                    @Argument(name = "x-max-priority", value = "10",type = "java.lang.Integer")
                            }
                    ),
                    exchange = @Exchange(name = "CSS_Exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "EAP_Reponse_CSS_RoutingKey"
            )
    )
    @RabbitHandler
    public void eapResponse(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============CSS_Exchange-EAP_Reponse_Queue=================,priority:"+message.getHeaders().get("priority")+",attr1"+message.getHeaders().get("attr1"));
        Thread.sleep(100);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        //模拟异常 ，然后 拒签消息 ，然后丢到死信队列
        try {
            System.err.println("处理消息的时候 出现异常，然后 拒签消息 ，然后丢到死信队列");
            throw new Exception("12333");
        }catch (Exception e){
            // 第一个false 是 不批量签收；第二个false 是 不重回队列
            channel.basicNack(deliveryTag,false,false);
        }

        //注意 这里特别注意 已经拒收的消息 再签收是要出错的，这里要特别注意。
        //channel.basicAck(deliveryTag,false);

    }
}
